package com.broadengate.view.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.broadengate.bean.RuleInfo;
import com.broadengate.service.UserService;
import com.broadengate.util.JSONResult;
import com.broadengate.view.pageModel.RuleTreeGrid;

@Controller
@RequestMapping("/RuleController")
public class RuleController
{
    private final static Logger log = Logger.getLogger(LoginController.class);
    
    @Autowired
    private UserService userService;
    
    @RequestMapping("/getRuleTreeGridByUserId")
    @ResponseBody
    public List<RuleTreeGrid> getRuleTreeGridByUserId(@RequestParam(value = "userId") int userId)
    {
        if (userId == 0)
        {
            userId = 1;
        }
        RuleTreeGrid rtg = userService.getRuleTreeGridByUserId(userId);
        List<RuleTreeGrid> result = new ArrayList<RuleTreeGrid>();
        result.add(rtg);
        return result;
    }
    
    @RequestMapping("/editUserRule")
    @ResponseBody
    public JSONResult editUserRule(RuleInfo info)
    {
        JSONResult result = new JSONResult();
        result.setSuccess(true);
        try
        {
            userService.editUserRule(info);
        }
        catch (SQLException e)
        {
            result.setSuccess(false);
            result.setMsg(e.getMessage());
        }
        result.setMsg("更新成功");
        return result;
    }
}
